﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace CPPEI.Coolzon.UI.XProperty.Converters
{
    [ValueConversion(typeof(String), typeof(int))]
    public class XStringToIntConverter : IPropertyValueConverter
    {
        public object Convert(ConvertContext context, Metadata.ValueNode valueNode)
        {
            int value = 0;
            if (int.TryParse(valueNode.Attributes["Value"], out value))
            {
                return value;
            }
            return null;
        }

        public Metadata.ValueNode ConvertBack(ConvertContext context, object value)
        {
            Metadata.ValueNode valueNode = new Metadata.ValueNode("Int");
            valueNode.Attributes = new Dictionary<string, string>();
            valueNode.Attributes.Add("Value", System.Convert.ToString(value));
            return valueNode;
        }
    }
}
